(function(){
return {
 version : "0.0.6",
 url : "http://vuzzle.googlecode.com/svn/release/usermodules/zodiac.js",
 updateUrl : "http://vuzzle.googlecode.com/svn/release/usermodules/versions/zodiac.version",
 name : "Зодиак",
 title : "Zodiac",
 description : "Показывает знак зодиака и возраст в профайле",
 author : "<a href='http://vkontakte.ru/drumtheatre'>DRUMtheatre</a>",
 depends : [],
 onApplySettings : function() {},
 onUninstall : function() {},
 init : function() {

if (vu.vk.page.isProfile) {

var born = new Date();

/*
calculates tropical zodiac sign
input:  month and day
output: tropical zodiac sign with label as string
see http://en.wikipedia.org/wiki/Signs_of_the_Zodiac
*/
function zodiac(day, month) {
        var sign;
        
        // link the month and day to the sign
        if      (day >= 21 && month ==  3 || day <= 19 && month ==  4) sign = "Овен ♈";
        else if (day >= 20 && month ==  4 || day <= 20 && month ==  5) sign = "Телец ♉";
        else if (day >= 21 && month ==  5 || day <= 20 && month ==  6) sign = "Близнецы ♊";
        else if (day >= 21 && month ==  6 || day <= 22 && month ==  7) sign = "Рак ♋";
        else if (day >= 23 && month ==  7 || day <= 22 && month ==  8) sign = "Лев ♌";
        else if (day >= 23 && month ==  8 || day <= 22 && month ==  9) sign = "Дева ♍";
        else if (day >= 23 && month ==  9 || day <= 22 && month == 10) sign = "Весы ♎";
        else if (day >= 23 && month == 10 || day <= 21 && month == 11) sign = "Скорпион ♏";
        else if (day >= 22 && month == 11 || day <= 21 && month == 12) sign = "Стрелец ♐";
        else if (day >= 22 && month == 12 || day <= 19 && month ==  1) sign = "Козерог ♑";
        else if (day >= 20 && month ==  1 || day <= 18 && month ==  2) sign = "Водолей ♒";
        else if (day >= 19 && month ==  2 || day <= 20 && month ==  3) sign = "Рыбы ♓";

        // return text with label
        return sign;
}

/*
add sign to page
input: date person is born
*/
function addSign() {

	    // find place to stick the info
        var links = document.evaluate("//a[contains(@href,'[bday]')]", document, null, 
                                       XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

        // loop over all dates
        var link = links.snapshotItem(0);

        // create a containers
        var addon1 = document.createElement("span");
        addon1.style.color = "#808080";
        addon1.setAttribute("id", "zodiac");
 
        var addon2 = document.createElement("span");
        addon2.setAttribute("id", "zodiac");
        
        var zZz = zodiac(born.getDate(), born.getMonth() + 1);

        // fill a containers
        addon1.innerHTML = " | ";
        addon2.innerHTML = zZz;

        // attach its
        link.parentNode.insertBefore(addon1, link.previousSibling);
        link.parentNode.insertBefore(addon2, link.previousSibling);
}

/*
add age of person to page
input: date person is born
*/
function addAge() {

	    // find the difference between two times
        var age = new Date() - born.getTime();

        // convert difference into years
        age = age / (1000 * 60 * 60 * 24 * 365.242199);

        // get nice values
        var years =  Math.floor(age);
        var months = Math.floor((age - years) * 10);

        // try to determine 'word'
        var word;
        if (years <= 111) {
            var x = Math.floor(years / 10);
            var y = years - 10 * x;
            if ((years < 1) || ((years > 1) && (years < 5))) word = " года";
            else if (((years > 21) && ((y > 1) && (y < 5))) || ((y == 1) && (months != 0))) word = " года";
            else if (((y == 1) && (months == 0)) && ((years != 11) && (years != 111))) word = " год";
            else word = " лет";

            // don't show 0 month
            var month$;
            months > 0 ? month$ = "," + months : month$ = "";

            // loop over the tag involving dates
            var links = document.evaluate("//a[contains(@href,'[bday]')]", document, null, 
                                           XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

            // loop over all dates
            var link = links.snapshotItem(0);

            // create a container
            var addon3 = document.createElement("span");
            addon3.setAttribute("style", "margin-left:4px;");
            addon3.setAttribute("id", "zodiac");

            // fill a container
            addon3.innerHTML = " " + years + month$ + word;

            // attach it
            link.parentNode.insertBefore(addon3, link.previousSibling);
        }
}

// get year from profile page
function getYear() {

	    // loop over the tag involving dates
        var y_links = document.evaluate("//a[contains(@href,'[byear]')]", document, null,
                                         XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

        // loop over all dates
        for (var i = 0; i < y_links.snapshotLength; i++) {
             var y_link = y_links.snapshotItem(i);
             var y_href = new String(y_link.getAttribute("href"));

             // extract a year
             if (y_href.indexOf('[byear]') != -1) {

                // extract actual data
                born.setFullYear(y_href.match(/\d{1,4}/g));
                addAge();
             }
        }
}

// get dates from profile page
function getDates() {

	    // loop over the tag involving dates
        var d_links = document.evaluate("//a[contains(@href,'[bday]')]", document, null, 
                                            XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

        // loop over all dates
        for (var i = 0; i < d_links.snapshotLength; i++) {
             var d_link = d_links.snapshotItem(i);
             var d_href = new String(d_link.getAttribute("href"));

             // extract date and month
             if ((d_href.indexOf('[bday]') != -1)) {

               	// extract actual data
                born.setMonth(parseFloat((d_href.match(/\d{1,2}/g)[1])) - 1);
                born.setDate(d_href.match(/\d{1,2}/g)[0]);
                addSign();
             }
        }
        getYear();
}

getDates();

// checking for the existence
function checkExist() {
	    var z = document.getElementById("zodiac");
	    if (!z) getDates();
}

// AJAX rebuilding function
function rebuild() {
	    var watch = document.getElementById("page_layout");
	    watch.addEventListener("DOMNodeInserted", function(e) {
	          if (e.target.className == "profile_info" || e.target.id == "wrap2") checkExist()}, false);
}

rebuild();
} 	
}
}})();